<template>
	<view class="page">
		<view class="form-content">
			<u-form :model="formData" ref="uForm" class="uForm">
				<u-form-item prop="month" label="月份" label-width="auto">
					<u-input v-model="formData.month" placeholder="请输入月份" input-align="right" />
				</u-form-item>
				<u-form-item prop="totalAssets" label="总资产" label-width="auto">
					<u-input v-model="formData.totalAssets" placeholder="请输入总资产" type="digit" input-align="right"
						@blur="handleAssetLiabilityRatio" />
					<view slot="right">万元</view>
				</u-form-item>
				<u-form-item prop="totalLiabilities" label="总负债" label-width="auto">
					<u-input v-model="formData.totalLiabilities" placeholder="请输入总负债" type="digit" input-align="right"
						@blur="handleAssetLiabilityRatio" />
					<view slot="right">万元</view>
				</u-form-item>
				<u-form-item prop="assetLiabilityRatio" label="资产负债率" label-width="auto">
					<u-input v-model="formData.assetLiabilityRatio" placeholder="请输入资产负债率" type="digit"
						input-align="right" />
					<view slot="right">%</view>
				</u-form-item>
				<u-form-item prop="salesRevenue" label="销售收入" label-width="auto">
					<u-input v-model="formData.salesRevenue" placeholder="请输入销售收入" type="digit" input-align="right"
						@blur="handleNetProfitMargin" />
					<view slot="right">万元</view>
				</u-form-item>
				<u-form-item prop="netProfit" label="净利润" label-width="auto">
					<u-input v-model="formData.netProfit" placeholder="请输入净利润" type="digit" input-align="right"
						@blur="handleNetProfitMargin" />
					<view slot="right">万元</view>
				</u-form-item>
				<u-form-item prop="netProfitMargin" label="净利润率" label-width="auto">
					<u-input v-model="formData.netProfitMargin" placeholder="请输入净利润率" type="digit"
						input-align="right" />
					<view slot="right">%</view>
				</u-form-item>
				<u-form-item prop="ownerEquity" label="所有者权益" label-width="auto">
					<u-input v-model="formData.ownerEquity" placeholder="请输入所有者权益" type="digit" input-align="right" />
					<view slot="right">万元</view>
				</u-form-item>
				<u-form-item prop="originalAssets" label="固定资产原值" label-width="auto">
					<u-input v-model="formData.originalAssets" placeholder="请输入固定资产原值" type="digit"
						input-align="right" />
					<view slot="right">万元</view>
				</u-form-item>
				<u-form-item prop="netAssets" label="固定资产净值" label-width="auto">
					<u-input v-model="formData.netAssets" placeholder="请输入固定资产净值" type="digit" input-align="right" />
					<view slot="right">万元</view>
				</u-form-item>
				<u-form-item prop="attachment" label="附件信息" label-width="auto" label-position="top">
					<JNPFUpload v-model="attendanceFileList" :fileSize='2' :limit='10' :showLabel="false"
						@uploading="uploading">
					</JNPFUpload>
				</u-form-item>

			</u-form>
		</view>
		<view class="save-bottom">
			<view class="fixed-btn save-padding">
				<u-button type="primary" shape="circle" :disabled="btnDisabled" @click="onSubmit">确定</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	const apiUrl = "/api/Jiushi/Customerfinancial";
	export default {
		components: {},
		name: 'financeInfo',
		data() {
			return {
				formData: {
					id: '',
					customerId: undefined,
					month: undefined,
					totalAssets: undefined,
					totalLiabilities: undefined,
					ownerEquity: undefined,
					assetLiabilityRatio: undefined,
					salesRevenue: undefined,
					netProfit: undefined,
					netProfitMargin: undefined,
					originalAssets: undefined,
					netAssets: undefined,
					attachment: undefined
				},
				attendanceFileList: [],
				btnDisabled: false
			}
		},
		onLoad(options) {
			if (options.cid) {
				this.formData.customerId = options.cid
			}
			if (options.id) {
				this.formData.id = options.id
				this.init()
			}
		},
		methods: {
			handleAssetLiabilityRatio() {
				if (this.formData.totalLiabilities != undefined && this.formData.totalAssets != undefined) {
					this.formData.assetLiabilityRatio = (parseFloat(this.formData.totalLiabilities / this.formData
						.totalAssets) * 100).toFixed(2)
				}
			},
			handleNetProfitMargin() {
				if (this.formData.netProfit != undefined && this.formData.salesRevenue != undefined) {
					this.formData.netProfitMargin = (parseFloat(this.formData.netProfit / this.formData
						.salesRevenue) * 100).toFixed(2)
				}
			},
			uploading(flag) {
				this.btnDisabled = flag
			},
			/* 初始化请求 */
			init() {
				this.$helper.ajax({
					url: apiUrl + "/" + this.formData.id,
					method: "GET",
					contentType: "application/json",
					success: res => {
						this.formData = res.data.data
						if (this.formData.attachment) {
							this.attendanceFileList = res.data.data.attachment.split(',').map(item => {
								let tempItem = {
									url: item
								}
								return tempItem
							})
						}
					}
				});
			},
			/* 表单提交 */
			onSubmit() {
				this.formData.attachment = this.attendanceFileList.map(item => {
					return item.url
				}).join(',')
				let url = apiUrl;
				let method = "POST";
				if (this.formData.id) {
					url = url + "/" + this.formData.id;
					method = "PUT";
				}
				this.$helper.ajax({
					url: url,
					method: method,
					contentType: "application/json",
					data: this.formData,
					success: res => {
						this.$helper.msg('保存成功', 'success');
						uni.setStorage({
							key: "refreshPage",
							data: true
						})
						uni.navigateBack();
					}
				});
			},
		}
	}
</script>

<style>

</style>